package com.jsoft.controller;

import com.alibaba.fastjson.JSON;
import com.jsoft.entity.TicketOpen;
import com.jsoft.service.TicketOpenService;
import com.jsoft.service.impl.TicketOpenServiceImpl;
import com.jsoft.utils.MD5Util;
import com.jsoft.utils.NoUtil;
import com.jsoft.utils.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author snkstart
 * @create 2023-02-21 20:33
 */
@WebServlet("*.to")
@MultipartConfig
public class TicketOpenController extends HttpServlet {
    private TicketOpenService ticketOpenService=new TicketOpenServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1,servletPath.length() - 3);

        try {
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    protected void saveTicketOpen(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        PrintWriter writer = resp.getWriter();
        //获取前端传的属性
        String enterpriseId = req.getParameter("enterpriseId");
        String acquirerId = req.getParameter("acquirerId");
        String amountstr = req.getParameter("amount");
        String institutyIdStr = req.getParameter("institutyId");
        String createTimeStr = req.getParameter("createTime");
        String expiryTimeStr = req.getParameter("expiryTime");
        String remark = req.getParameter("remark");

        //付息方式
        String paymentInterestType = req.getParameter("paymentInterestType");
        //票据状态
        String status="A";
        //上链地址
        String salt = MD5Util.getSalt();
        //将金钱的类型进行转换
        double amount = Double.parseDouble(amountstr);
        //获取编号
        String no = NoUtil.getNo();
        int institutyId = Integer.parseInt(institutyIdStr);
        //将接到的日期格式转换
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date createTime = simpleDateFormat.parse(createTimeStr);
        Date expiryTime = simpleDateFormat.parse(expiryTimeStr);

        //将属性放入对象中并调用添加方法
        TicketOpen ticketOpen = new TicketOpen(no,enterpriseId,acquirerId,amount,institutyId,createTime,expiryTime,paymentInterestType,status,salt,remark);
        boolean res = ticketOpenService.saveTicketOpen(ticketOpen);
        if(res){
            writer.write(JSON.toJSONString(new ResultUtil("200","提交成功")));
        }else {
            writer.write(JSON.toJSONString(new ResultUtil("201","提交失败")));
        }

        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
